package com.woniuxy.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 创建线程的第三种方式
 *
 * @author 老谭 (<a href="http://www.woniuxy.com">蜗牛学苑</a>)
 *
 */
class Thread3 implements Callable<String> {

	@Override
	public String call() throws Exception {
		// 模拟耗时任务
		Thread.sleep(2000);
		return "Hello";
	}

}

public class ThreadTest3 {

	public static void main(String[] args) {
		Thread3 thread3 = new Thread3();

		FutureTask<String> futureTask = new FutureTask<>(thread3);
		futureTask.run();
		try {
			String r = futureTask.get();
			System.out.println(r);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}

}
